package com.fancy.projecty.controller;

import com.fancy.projecty.mapper.WaterMenuMapper;
import com.fancy.projecty.pojo.WaterMenu;
import com.fancy.projecty.pojo.bo.LoginInfoBO;
import com.fancy.projecty.service.WaterMenuService;
import com.fancy.projecty.service.WaterRoleService;

import com.fancy.projecty.util.JwtTokenUtil;
import com.fancy.projecty.util.ResultVO;
import com.fancy.projecty.util.SuccessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RestController
@RequestMapping
@CrossOrigin
@Api(tags = "登录功能")
public class LoginController {

	@GetMapping("/")
	public String index() {
		return "I am Project";
	}

	public static final ThreadLocal<LoginInfoBO> loginInfoThreadLocal = new ThreadLocal<LoginInfoBO>();

	@ApiOperation(value = "登录功能",
			notes = "{\"username\":\"admin\",\"password\":\"admin\"} ")
	@CrossOrigin
	@PostMapping("/login")
	public ResultVO login(@RequestBody Map<String, String> map) {

		String username = map.get("username");
		String password = map.get("password");

		UsernamePasswordToken token = new UsernamePasswordToken(username, password);

		Subject currentUser = SecurityUtils.getSubject();

		try {
			//主体提交登录请求到SecurityManager
			currentUser.login(token);

		} catch (IncorrectCredentialsException ice) {

			return new ResultVO(false, 400, "密码不正确");
		} catch (UnknownAccountException uae) {
			return new ResultVO(false, 401, "账号不存在");
		} catch (AuthenticationException ae) {
			// 如果身份校验失败了.
			return new ResultVO(false, 402, ae.getMessage());  //账号已经被禁用
		}

		if (currentUser.isAuthenticated()) {

			HashMap<Object, Object> claimsMap = new HashMap<>();

			LoginInfoBO loginInfoBO = loginInfoThreadLocal.get();
			loginInfoThreadLocal.remove();// 清空


			//签发token
			String jwtToken = JwtTokenUtil.generateToken(claimsMap, username);
			loginInfoBO.setJwtToken(jwtToken);
			return new ResultVO(true, 200, "成功", loginInfoBO);

		}
		return null;
	}


	@GetMapping("/logout")
	public SuccessVo logout() {

		try {
			Subject currentUser = SecurityUtils.getSubject();
			currentUser.logout();
		} catch (Exception e) {
		}

		return new SuccessVo();


	}

}


